'''
description: 列表的操作
Version: 1.0
Author: Victor
Date: 2023-4-18

'''

def ListFunc_len():
    '''
    len函数访问列表长度
    '''
    pass

def ListFunc_append():
    '''
    append函数向列表末尾添加元素，无返回值
    '''
    append_list = [1, 2, 3]
    append_list.append(4)
    print("列表元素", append_list)

    append_list=["hello","world"]
    append_list.append("python")
    print("列表元素",append_list)

def ListFunc_insert():
    '''
    insert()函数向列表中插入元素，无返回值
    '''

    insert_list=[1,2,3]
    insert_list.insert(2,10)
    print("列表元素",insert_list)

    insert_list=["hello","world"]
    insert_list.insert(0,"python")
    print("列表元素",insert_list)

def ListFunc_extend():
    '''
    extend函数在列表的末尾添加另一个列表
    '''

    list_a = [1,2,3]
    list_b = [4,5,6]
    list_a.extend(list_b)

    print("输出列表list_a",list_a)

def ListFunc_del():
    '''
    del：可以直接删除列表，也可以删除指定下标数据
    '''
    pass

def ListFunc_remove():
    '''
    remove函数从列表移除元素，无返回值
    '''

    remove_list = [1,2,3]
    remove_list.remove(3)
    
    print("输出列表remove_list：",remove_list)

    remove_list=["hello","world","python","hello"]
    remove_list.remove("hello")
    print("输出列表remove_list：",remove_list)

def ListFunc_pop():
    '''
    pop函数移除列表中指定位置的元素，并返回要移除的元素。
    '''
    pop_list = ["hello","world","python","hello"]
    elem = pop_list.pop(1)
    print("移除的元素是：",elem)
    print("移除元素后的列表：",pop_list)


def ListFunc_clear():
    '''
    clear函数用于将列表清空
    '''
    clear_list = ["hello","world","python","hello"]
    clear_list.clear()

    print("输出清空后的列表：",clear_list)
    print("输出清空后的列表长度：",len(clear_list))

def ListFunc_reverse():
    '''
    reverse函数用于将列表反向排列
    '''
    reverse_list = ["hello","world","python","hello"]
    reverse_list.reverse()
    print("输出反向排列的列表：",reverse_list)

def ListFunc_sort():
    '''
    sort函数用于将列表进行排序
    '''
    sort_list = ["hello","world","hello","python"]
    # 默认进行升序排列
    sort_list.sort()
    print("将列表进行升序排序：",sort_list)

    sort_list = [1,9,6,7]
    # 降序排列
    sort_list.sort(reverse=True)
    print("对列表进行降序排序：",sort_list)

    sort_list=[{"price":10.5},
                {"price":24},
                {"price":15}]

    sort_list.sort(key=lambda item:item["price"],reverse=True)
    print("对列表中数据按指定条件进行降序排序：\n",sort_list)


def ListFunc_copy():
    '''
    copy函数用于创建列表的副本
    '''
    list_a=["hello","world","hello","python"]
    list_b = list_a.copy()
    print("创建list_a的一个副本并赋值给list_b：",list_b)

    del list_a[0]
    print("删除list_a[0]元素后，在输出list_b：",list_b)

    list_c = ["hello","world","hello","python"]
    list_d = list_c
    del list_c[0]
    print("删除list_c[0]元素后，再输出list_d：",list_d)


def ListFunc_index():
    '''
    index函数用于返回所匹配的元素的索引
    '''
    index_list = ["hello","world","hello","python"]
    result = index_list.index("hello")
    print("hello在列表中的索引位置：",result)

    result = index_list.index("hello",1,3)
    print("hello在列表中的索引位置：",result)

def ListFunc_count():
    '''
    count函数用于统计某个元素在列表中出现的次数
    '''
    count_list = ["hello","world","hello","python"]
    result = count_list.count("world")
    print("world在列表中的个数：",result)

    result = count_list.count("hello")
    print("hello在列表中的个数：",result)

def ListFunc_count():
    '''
    count函数用于统计某个元素在列表中出现的次数
    '''
    count_list = ["hello","world","hello","python"]
    result = count_list.count("world")
    print("world在列表中的个数：",result)

    result = count_list.count("hello")
    print("hello在列表中的个数：",result)

def ListFunc_traversal():
    '''
    列表遍历
    '''
    # while循环：
    i = 0
    traversal_list = ["hello","world","hello","python"]
    while i < len(traversal_list):
        print(traversal_list[i])
        i += 1

    # for循环：

    for i in range(len(traversal_list)):
        print(i,  end=' ')
        print(traversal_list[i])

    for ele in traversal_list:
        print(traversal_list.index(ele), end=' ')
        print(ele)

    for i, s in enumerate(traversal_list):
        print(i, end=' ')
        print(s)


def ListFunc_nesting():
    '''
    列表嵌套
    '''
    nesting_list =[['A', 'G', 'H'], ['E', 'F'], ['B', 'C', 'D']]
    for j in nesting_list:
        for k in j:
            print(k,end='')
        print()


if __name__ == '__main__':
    ListFunc_len()
    ListFunc_append()
    ListFunc_insert()
    ListFunc_extend()
    ListFunc_del()
    ListFunc_remove()
    ListFunc_pop()
    ListFunc_clear()
    ListFunc_reverse()
    ListFunc_sort()
    ListFunc_copy()
    ListFunc_index()
    ListFunc_count()
    ListFunc_traversal()
    ListFunc_nesting()




 
